/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package umled.model;

import java.awt.Graphics2D;
import java.util.*;
import org.w3c.dom.*;
/**
 *
 * @author ANor
 */
public class UMLPackage extends UMLElement{
    private final List<UMLPackage> innerPackages = new ArrayList<UMLPackage>();
    private final List<UMLType> types = new ArrayList<UMLType>();
    
    public UMLPackage(String s, UMLElement parent, int x, int y, int w, int h){
        super(s, parent, x, y, w, h);
    }
    public List<UMLPackage> getInnerPackages(){
        if( innerPackages == null)
            return null;
        return Collections.unmodifiableList(innerPackages);
    }
    public void addPackage(UMLPackage pac){
        innerPackages.add(pac);
    }
    public void removePackage(UMLPackage pac){
        innerPackages.remove(pac);
    }
    public List<UMLType> getTypes(){
        if( types == null)
            return null;
        return Collections.unmodifiableList(types);
    }
    public void addType(UMLType type){
        types.add(type);
    }
    public void removeType(UMLType type){
        types.remove(type);
    }
    public void draw(Graphics2D g2d, int x, int y){
        int xDr = Math.min( x, getX() + getWidth());
        int yDr = Math.min( y, getY() + getHeight());
        drawRectangle( g2d, getX(), getY(), getX() + getWidth() , getY() + getHeight(), xDr, yDr );
        drawString( g2d, getName(), getX()+10, getY()+10 , x, y );
        drawRectangle( g2d, getX(), getY(), getX() + getName().length()*8 + 5 , getY() + 12 , xDr, yDr );
        for(UMLElement e: innerPackages)
            e.draw(g2d, xDr, yDr);
        for(UMLElement e: types)
            e.draw(g2d, xDr, yDr);
    }
    @Override
    public Element getXML( Document document ){
        Element element = super.getXML( document );
        element.setAttribute( "type" , "UMLPackage" );
        
        Element typesList = document.createElement( "Types" );
        for( UMLType type : types)
            typesList.appendChild( type.getXML(document) );
        element.appendChild( typesList );
        
        Element packagesList = document.createElement( "Packages" );
        for( UMLPackage currentPackage : innerPackages )
            typesList.appendChild( currentPackage.getXML(document) );
        element.appendChild( packagesList );
        
        return element;
    }
}
